﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SetupLauncher.Domain.Util;

namespace SetupLauncher.Domain.Logic.Environment
{
    internal class PrepareSinacor : IPrepareCommand
    {
        Logger.ILoggable _log;

        public PrepareSinacor(Logger.ILoggable log) 
        {
            _log = log;
        }

        public void Execute()
        {
            try
            {
                List<string> commands = new List<string>();

                commands.Add("sc stop \"STS Hoster Service\"");
                commands.Add("sc delete \"STS Hoster Service\"");
                commands.Add("sc stop \"SinacorLoggingService\"");
                commands.Add("sc delete \"SinacorLoggingService\"");

                commands.Add(@"del c:\windows\assembly\Bmf.Corporativo.CPL.ServiceProxy.dll");
                commands.Add(@"del c:\windows\assembly\CrystalDecisions.CrystalReports.Engine.dll");
                commands.Add(@"del c:\windows\assembly\CrystalDecisions.Shared.dll");
                commands.Add(@"del c:\windows\assembly\CrystalDecisions.Windows.Forms.dll");
                commands.Add(@"del c:\windows\assembly\Dynamite.dll");
                commands.Add(@"del c:\windows\assembly\WPFToolkit.dll");
                commands.Add(@"del c:\windows\assembly\Sinacor*.* /s ");
                commands.Add(@"del c:\windows\assembly\Bovespa*.* /s");
                commands.Add(@"del c:\windows\assembly\Bvmf*.* /s ");

                string data = DateTime.Now.ToString("ddMMyyyyhhmmss");
                commands.Add(@"rename C:\DSEPROJ\Compilados\Sinacor Sinacor_old_" + data);

                foreach (string command in commands)
                {
                    WriteLog("Executando o comando: " + command);
                    DOSCommand.Execute(command);
                }

            }
            catch (Exception ex)
            {
                WriteException(ex.Message);
                throw ex;
            }
        }

        private void WriteLog(string message)
        {
            if (_log == null)
                return;
            _log.Write(message);
        }

        private void WriteException(string message)
        {
            if (_log == null)
                return;
            _log.WriteException(message);
        }
    }
}
